<script setup>
import { ref,computed } from 'vue'
import {useStudyRoomStore} from "@/stores/index.js";
const store = useStudyRoomStore();
const closeWidget = () => {
  store.widgetSwitch.calendar = false;
};
const currentDate = new Date();
const currentYear = ref(currentDate.getFullYear());
const currentMonth = ref(currentDate.getMonth());

const daysOfWeek = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
const months = ['January', 'February', 'March', 'April', 'May', 'June',
  'July', 'August', 'September', 'October', 'November', 'December'];

const createCalendar = () => {
  let firstDayOfMonth = new Date(currentYear.value, currentMonth.value, 1).getDay();
  let lastDateOfMonth = new Date(currentYear.value, currentMonth.value + 1, 0).getDate();

  let monthDates = [];

  // 在网格的开头填充上个月的日期(空)
  for (let i = firstDayOfMonth - 1; i >= 0; i--) {
    monthDates.push('');
  }

  // 填写当月的日期
  for (let i = 1; i <= lastDateOfMonth; i++) {
    monthDates.push(i);
  }

  // 确保日历网格有 6 周
  while (monthDates.length % 42 !== 0) {
    monthDates.push('');
  }

  return monthDates;
};

const calendarBody = computed(() => {
  let dates = createCalendar();
  let weeks = [];
  while (dates.length > 0) weeks.push(dates.splice(0, 7));
  return weeks;
});

const monthYear = computed(() => {
  return `${months[currentMonth.value]} ${currentYear.value}`;
});

const isCurrentDay = (day) => {
  let today = new Date();
  return day === today.getDate() && currentMonth.value === today.getMonth() && currentYear.value === today.getFullYear();
};
</script>

<template>
<!-- 再三决定组件重写，不用element ui了 -->
  <div class="calendar">

    <div class="header">
      <div class="month-year">{{ monthYear }}</div>
      <div class="two-icon">
        <img src="../../assets/icon/StudyRoom/smile.png" alt="" class="icon1" />
        <img src="../../assets/icon/StudyRoom/exit.png" alt="" class="icon2" @click="closeWidget" />
      </div>
    </div>

    <table>

      <thead class="days-of-week">
      <tr>
        <th v-for="day in daysOfWeek" :key="day">{{ day }}</th>
      </tr>
      </thead>


      <tbody>
      <tr v-for="(week, index) in calendarBody" :key="index">
        <td v-for="(day, idx) in week" :key="idx" :class="{'current-day': isCurrentDay(day)}">
          {{ day }}
        </td>
      </tr>
      </tbody>
    </table>
  </div>

</template>

<style scoped lang="scss">
.calendar {
  position: relative;
  left: 20px;
  top: 100px;
  width: 300px;
  border-radius: 20px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.3);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-end;

  .header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 7px 12px;
    background-color: rgba(255, 255, 255, 0.8);
    .month-year {
      font-size: 18px;
      font-weight: bold;
    }
    .two-icon {
      width: 55px;
      height: 20px;
      display: flex;
      align-items: center;
      justify-content: space-around;
      gap: 15px;
      .icon1,
      .icon2 {
        width: 20px;
        height: 20px;
      }
    }
  }

  table {
    width: 100%;
    border-collapse: collapse;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding-bottom: 8px;
    .days-of-week {
      width: 100%;
      border-bottom: white 1px solid;
      tr{
        display: flex;
        justify-content: space-around;
        align-items: center;
        padding-left: 14px;
        padding-right: 14px;
        margin-top: 5px;
        margin-bottom: 5px;
        th{
          font-size: 16px;
          font-weight: 400;
        }
      }
    }
    th, td {
      width: 14.28%;
      padding: 10px;
      text-align: center;

      &.current-day {
        background-color: #3d9dff;
        border-radius: 8px;
        color: white;
      }
    }
  }
}

</style>
